package com.cirkana.viewer.data.route.gpx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.List;

import com.cirkana.data.route.gpx.IGpxRoute;
import com.cirkana.data.route.gpx.IGpxRouteMetadata;
import com.cirkana.data.route.gpx.IGpxRouteTrack;
import com.cirkana.data.route.gpx.IGpxRouteWayPoint;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.serial.bin.IBinarySerializable;

public class GpxRoute implements IGpxRoute, IBinarySerializable {

	private IGpxRouteMetadata metadata;
	private List<IGpxRouteWayPoint> wayPoints;
	private List<IGpxRouteTrack> tracks;

	@Override
	public void setMetadata(IGpxRouteMetadata metadata) {
		this.metadata = metadata;
	}

	@Override
	public IGpxRouteMetadata getMetadata() {
		return metadata;
	}

	@Override
	public List<IGpxRouteWayPoint> getWayPoints() {
		return this.wayPoints;
	}

	@Override
	public void setWayPoints(List<IGpxRouteWayPoint> wayPoints) {
		this.wayPoints = wayPoints;
	}

	@Override
	public List<IGpxRouteTrack> getTracks() {
		return tracks;
	}

	@Override
	public void setTracks(List<IGpxRouteTrack> tracks) {
		this.tracks = tracks;
	}

	// TODO: Should be protected
	public GpxRoute() {
	}

	@Override
	public void serialize(DataOutputStream out) throws BinarySerializationException {
		BinarySerializationUtil.serializeSerializable((IBinarySerializable) this.metadata, out);
		BinarySerializationUtil.serializeSerializableList(this.wayPoints, out);
		BinarySerializationUtil.serializeSerializableList(this.tracks, out);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void deserialize(DataInputStream in) throws BinarySerializationException {
		this.metadata = (GpxRouteMetadata) BinarySerializationUtil.deserializeSerializable(GpxRouteMetadata.class, in);
		this.wayPoints = (List<IGpxRouteWayPoint>) BinarySerializationUtil.deserializeSerializableList(GpxRouteWayPoint.class, in);
		this.tracks = (List<IGpxRouteTrack>) BinarySerializationUtil.deserializeSerializableList(GpxRouteTrack.class, in);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("{");
		s.append("metadata=");
		if (this.metadata == null)
			s.append("(null)");
		else
			s.append(metadata.toString());
		s.append(",wayPoints=");
		if (this.wayPoints == null)
			s.append("(null)");
		else {
			s.append("{");
			for (IGpxRouteWayPoint wayPoint : this.wayPoints) {
				if (wayPoint == null)
					s.append("(null)");
				else
					s.append(wayPoint.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}
		s.append(",tracks=");
		if (this.tracks == null)
			s.append("(null)");
		else {
			s.append("{");
			for (IGpxRouteTrack track : this.tracks) {
				if (track == null)
					s.append("(null)");
				else
					s.append(track.toString());
				s.append(",");
			}
			s.delete(s.length() - 1, s.length());
			s.append("}");
		}

		s.append("}");
		return s.toString();
	}

}
